using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;

namespace Sedna.Util
{
    public class FileDownloader
    {
        private FileDownloader() { }

        public static bool WriteIn(HttpResponse response, String path)
        {
            if (response == null)
            {
                throw new ArgumentNullException("Response is null.");
            }

            if (String.IsNullOrEmpty(path))
            {
                throw new ArgumentException("Path is null or empty.");
            }



            if (!File.Exists(path))
                return false;

            FileInfo file = new FileInfo(path);

            byte[] data;

            using (FileStream stream = file.OpenRead())
            {
                data = new byte[stream.Length];

                stream.Read(data, 0, (int)stream.Length);
            }

            response.Clear();
            response.AddHeader("Content-Type", "application/binary");
            response.AddHeader("Content-Length", data.Length.ToString());
            response.AddHeader("Content-Disposition", "attachment;filename=" + file.Name);
            //response.ContentType = "application/octet-stream";
            response.OutputStream.Write(data, 0, data.Length);
            response.Flush();
            response.End();

            return true;
        }
    }
}
